/*
 * Shop+
 * created by 2016-10-09
 */

;var Shop = Shop || {};

Vue.http.options.emulateJSON = true;
var baseUrl = "${interface_web_url}";
var icpsUrl = "${icps_web_url}";
var searchUrl = "${search_web_url}";
var ebuyUrl = "${ebuy_web_url}";
var DI = {
	queryCouponList: baseUrl+"/innerCoupon/queryCouponList.do", //券列表
	queryRemainTimes: baseUrl+"/innerCoupon/queryRemainTimes.do", //当月剩余次数
	checkMemerByMobile: baseUrl+"/innerCoupon/checkMemerByMobile.do", //校验手机号是否是会员
	sendCoupon: baseUrl+"/innerCoupon/sendCoupon.do", //发券
	couponRecord: baseUrl+"/innerCoupon/couponRecord.do", //发券记录
	login: baseUrl+"/innerCoupon/login.do", //跳转登录
	getStoreSaleDetail: baseUrl+"/storeman/getStoreSaleDetail.do?sort=-1", //门店销售明细
	getSaleStatistic: baseUrl+"/storeman/getSaleStatistic.do", //查询销售统计信息
	getStoreKeeperSaleData: baseUrl+"/storeman/getStoreKeeperSaleData.do",
	queryCouponByCommList: baseUrl+"/coupon/queryCouponByCommList.do", //根据商品编码列表查询优惠券集合
	queryCouponInfoByComm: baseUrl+"/coupon/queryCouponInfoByComm.do", //根据商品编码查询优惠券详情列表
	getAllPriceNew: icpsUrl+"/icps-web/", //商品价格查询
	searchCoupon: ebuyUrl+"/quan/", //券搜索接口
	searchGoods: searchUrl+"/", //商品搜索接口
	returnGoods: baseUrl + "/returnGoods/home.do"
};

Shop.getUrlArgs = function(){
    var args = {},
        query = location.search.substring(1),
        pairs = query.split("&"),
        pos = idx = argname = val = null;
    for(var i=0; i<pairs.length; i++){
        pos = pairs[i];
        idx = pos.indexOf("=");
        if(idx == -1) continue;
        argname = pos.substring(0, idx);
        val = decodeURIComponent(pos.substring(idx + 1));
        args[argname] = val;
    }
    return args;
};

Shop.sa = function(env){
     function _loadAsyncJs(src) {
        var _src = src;
        var _scripts = document.getElementsByTagName('script');
        for (var i = 0; i < _scripts.length; i++) {
            if (_scripts[i].src == _src) {
                return;
            }
        }
        var _script = document.createElement('script');
        _script.type = 'text/javascript';
        _script.async = true;
        _script.src = _src;
        var _s = _scripts[0];
        _s.parentNode.insertBefore(_script, _s);
    }
    //创建在页面引入外部js文件的script标签的方法结束

    //判断环境，设置js文件路径开始
    function _getJsFilePath(js_file) {
        var _hostName = document.location.hostname;
        // 一般生产环境的域名
        var _prd_reg = '.suning.com';
        // 一般pre环境的域名
        var _pre_reg = '.cnsuning.com';
        // 一般sit环境的域名
        var _sit_reg = '.cnsuning.com';
        //以上为一般情况下各种环境(sit,pre,prd)的域名格式，如果自己系统环境的域名格式不同，请根据实际情况制定

        var sa_src = '';
        if (sa.env=='prd' || _hostName.indexOf(_prd_reg) != -1) {
            sa_src = ('https:' == document.location.protocol) ? 'https://imgssl.suning.com' : 'http://script.suning.cn';
        } else if (sa.env=='pre'|| _hostName.indexOf(_pre_reg) != -1) {//sit系统
            sa_src = ('https:' == document.location.protocol) ? 'https://preimgssl.suning.com' : 'http://prescript.suning.cn';
        } else if (sa.env=='sit' || _hostName.indexOf(_sit_reg) != -1) {
            sa_src = ('https:' == document.location.protocol) ? 'https://sit1imgssl.suning.com' : 'http://sit1script.suning.cn';
        } else {
            sa_src = ('https:' == document.location.protocol) ? 'https://imgssl.suning.com' : 'http://script.suning.cn';
        }
        sa_src = sa_src + '/javascript/sn_da/' + js_file;
        return sa_src;
    }
    //判断环境，设置js文件路径结束

    //调用方法，加载js文件开始
    var sa = sa || {};
    sa.env = 'sit'; //env的取值范围'prd、pre和sit',请根据实际环境配置！上线前请配置prd
    _loadAsyncJs(_getJsFilePath('sa_simple.js'));
    //调用方法，加载js文件结束
};

var passport_config = {
        base: "/zzg-web/", 
        loginTheme: "MOBTS_WAP" 
    };

$(function(){
    // safari模拟a标签active状态
    var a = document.getElementsByTagName('a');
    for(var i = 0; i < a.length; i++){
        a[i].addEventListener('touchstart', function(){}, false);
    }
    
	Shop.sa();
});

// 获取图片地址
Shop.imgURL = function(partnumber, size) {
    var imgURL = 'img/product_def.png';
    if(partnumber && partnumber != ''){
        var imageZone = Math.floor(Math.random() * 4 + 1);
        size = size ? size : '800';
        imgURL = '//image' + imageZone + '.suning.cn/uimg/b2c/newcatentries/0000000000-000000000' + partnumber + '_1_' + size + 'x' + size + '.jpg';
    }
    return imgURL;
};

// 当图片404时 要展示一张默认的的图片
Shop.imgURL404 = function(self){
    self.src = 'img/product_def.png';
    self.onerror = null; //防止死循环
};

Vue.filter('imgURL', function(partnumber, index, size){
    var imgURL = 'img/product_def.png';
    if(partnumber && partnumber != ''){
        var imageZone = Math.floor(Math.random() * 4 + 1);
        index = index ? index : '1';
        size = size ? size : '800';
        imgURL = '//image' + imageZone + '.suning.cn/uimg/b2c/newcatentries/0000000000-000000000' + partnumber + '_' + index + '_' + size + 'x' + size + '.jpg';
    }
    return imgURL;
});

Vue.filter('priceType', function(val){
    if(val == "1"){
        return "自营"
    }else if(val == "3"){
    	return "直降"
    }else if(val == "4-1"){
    	return "大聚惠"
    }else if(val == "4-2"){
    	return "抢购"
    }else if(val == "4-3"){
    	return "团购"
    }else if(val == "4-4"){
    	return "闪购"
    }else if(val == "4-5"){
    	return "渠道专享"
    }else if(val == "4-6"){
    	return "名品特卖"
    }
});

Vue.filter('praiseRate', function(val){
    return val*100+"%"
});
